/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components;

import com.bff.bjj.ui.component.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import org.bff.slimserver.musicobjects.SlimPlaylistItem;

/**
 *
 * @author bfindeisen
 */
public class PlaylistCellRenderer extends DefaultListCellRenderer {

    private HashMap<String, PlaylistCellPanelIface> listPanels;
    private SlimPlaylistItem currentItem;

    public PlaylistCellRenderer() {
        listPanels = new HashMap<String, PlaylistCellPanelIface>();
    }

    @Override
    public Component getListCellRendererComponent(JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        //super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (!(value instanceof SlimPlaylistItem)) {
            return this;
        }

        SlimPlaylistItem object = (SlimPlaylistItem) value;

        PlaylistCellPanelIface pcp = getListPanels().get(object.getId());

        if (pcp != null) {
            if ((index % 2) == 0) {
                pcp.setEven();
            } else {
                pcp.setOdd();
            }

            if (!isSelected) {
                pcp.setForeground(ColorProperties.LIST_FOREGROUND);
                pcp.setNotSelected();
            } else {
                pcp.setSelected();
            }

            if (getCurrentItem() != null) {
                if (object.equals(getCurrentItem())) {
                    pcp.setCurrentPlaying();
                } else {
                    pcp.setNotPlaying();
                }
            } else {
                pcp.setNotPlaying();
            }
            return (pcp.getPanel());
        } else {
            pcp = new PlaylistCellPanel(object, list);
            getListPanels().put(object.getId(), pcp);
            return pcp.getPanel();
        }
    }

    /**
     * @return the listPanels
     */
    public HashMap<String, PlaylistCellPanelIface> getListPanels() {
        return listPanels;
    }

    /**
     * @return the currentItem
     */
    public SlimPlaylistItem getCurrentItem() {
        return currentItem;
    }

    /**
     * @param currentItem the currentItem to set
     */
    public void setCurrentItem(SlimPlaylistItem currentItem) {
        this.currentItem = currentItem;
    }
}
